package trash.classloader;

///*
// * To change this template, choose Tools | Templates
// * and open the template in the editor.
// */
//package jsystem.jbrowser.classloader;
//
//import java.io.File;
//import java.net.HttpURLConnection;
//import java.net.JarURLConnection;
//import java.net.MalformedURLException;
//import java.net.URI;
//import java.net.URL;
//import java.net.URLClassLoader;
//import java.util.Arrays;
//import java.util.logging.Level;
//import java.util.logging.Logger;
//import javax.tools.DiagnosticCollector;
//import javax.tools.JavaCompiler;
//import javax.tools.JavaFileObject;
//import javax.tools.StandardJavaFileManager;
//import javax.tools.StandardLocation;
//import javax.tools.ToolProvider;
//import jbrowser.JBrowser;
//import org.apache.bcel.classfile.ClassParser;
//import org.apache.bcel.classfile.JavaClass;
//import org.apache.commons.io.FileUtils;
//
///**
// *
// * @author buczko
// */
//public class ClassLoader {
//
//    private URLClassLoader urlClassLoader;
//
//    public ClassLoader() {
//        try {
//            URL[] urls = {JBrowser.getRepository().toURI().toURL()};
//            urlClassLoader = new URLClassLoader(urls);
//        } catch (MalformedURLException ex) {
//            Logger.getLogger(ClassLoader.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }
//
//    public Class load(URI uri) throws ClassLoadingException {
//       if (uri.getScheme().equals("file")) {
//            return loadClassFromFile(uri);
//        } else if (uri.getScheme().equals("http")) {
//            return loadClassFromHTTP(uri);
//        } else {
//            Logger.getLogger(ClassLoader.class.getName()).log(Level.SEVERE, "Can't load Class from URI: {0}", uri);
//            throw new ClassLoadingException("Can't load Class from URI: " + uri);
//        }
//    }
//
//    public Class loadClassFromFile(URI uri) throws ClassLoadingException {
//        if (uri.getHost() == null) {
//            return loadClassFromFile(new File(uri));
//        } else {
//            try {
//                String fileName = uri.getPath().substring(uri.getPath().lastIndexOf('/') + 1);
//                File file = new File(JBrowser.getTempFolder(), fileName);
//                FileUtils.copyURLToFile(uri.toURL(), file);
//                return loadClassFromFile(file);
//            } catch (Exception ex) {
//                throw new ClassLoadingException(ex);
//            }
//        }
//    }
//
//    public Class loadClassFromFile(File file) throws ClassLoadingException {
//        try {
//            if (file.exists() && file.isFile()) {
//                if (file.getName().endsWith(".class")) {
//                    ClassParser classParser;
//                    classParser = new ClassParser(file.getPath());
//                    JavaClass javaClass = classParser.parse();
//                    File repoFile = new File(JBrowser.getRepository(), javaClass.getClassName().replace('.', File.separatorChar).concat(".class"));
//                    FileUtils.copyFile(file, repoFile);
//                    return Class.forName(javaClass.getClassName(), true, urlClassLoader);
//                } else if (file.getName().endsWith(".java")) {
//                    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
//                    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
//                    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
//                    fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(JBrowser.getRepository()));
//                    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file);
//                    JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits);
//                    boolean success = task.call();
//                    fileManager.close();
//                    if (success) {
//                        return this.getClass();
//                    } else {
//                        throw new ClassLoadingException("Can't load file: " + file.getName());
//                    }
//                } else {
//                    throw new ClassLoadingException("Can't load file: " + file.getName());
//                }
//            } else {
//                throw new ClassLoadingException("Can't load file: " + file.getName());
//            }
//        } catch (Exception ex) {
//            throw new ClassLoadingException(ex);
//        }
//    }
//
//    public Class loadClassFromHTTP(URI uri) throws ClassLoadingException {
//        try {
//            HttpURLConnection connection = (HttpURLConnection) (uri.toURL().openConnection());
//            String fileName = uri.getPath().substring(uri.getPath().lastIndexOf('/') + 1);
//            // Atachment filename
//            String contentDisposition = connection.getHeaderField("Content-Disposition");
//            if (contentDisposition != null) {
//                for (String param : contentDisposition.replace(" ", "").split(";")) {
//                    if (param.startsWith("filename=")) {
//                        fileName = param.split("=", 2)[1];
//                        break;
//                    }
//                }
//            }
//            File file = new File(JBrowser.getTempFolder(), fileName);
//            FileUtils.copyInputStreamToFile(connection.getInputStream(), file);
//            return loadClassFromFile(file);
//        } catch (Exception ex) {
//            throw new ClassLoadingException(ex);
//        }
//    }
//}
